package com.artfii.fluentsql.example;

import act.controller.annotation.UrlContext;
import act.util.JsonView;
import com.artfii.fluentsql.core.Page;
import com.artfii.fluentsql.core.mock.User;
import com.artfii.fluentsql.jdbctemplate.Lq;
import com.artfii.fluentsql.jdbctemplate.Qe;
import org.osgl.mvc.annotation.GetAction;
import org.osgl.util.C;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Func :
 *
 * @author: leeton on 2019/6/28.
 */
@UrlContext("/test/jdbc")
public class TestJdbcController extends BaseController {

    @Inject
    private DemoService demoService;

    @GetAction()
    @JsonView
    public String hello(String name) {
       return demoService.sayHi(name);
    }

    @GetAction("user/{id}")
    @JsonView
    public User getUserById(Integer id) {
//        User user = new Lq<User>(User.class, jdbcTemplate).andEq(User::getId,id).debug().to();
        //测试 状态为list时
        User user = new Lq<User>(User.class, jdbcTemplate).andIn(User::getRole, C.newList(1,2)).debug().to();
        return user;
    }

    @GetAction("user")
    @JsonView
    public List<User> getUserList() {
        return new Lq<User>(User.class, jdbcTemplate).andGe(User::getId,0).debug().toList();
    }

    @GetAction("count")
    @JsonView
    public Long count() {
        return new Lq<User>(User.class, jdbcTemplate).toCount();
    }

    @GetAction("adduser/{id}")
    @JsonView
    @Transactional
    public User addUser(Integer id) {
        User user = new User();
        user.setId(id);
        user.setUserName("jack");
        user.setDeptId(1);
        user.setMoney(new BigDecimal(1000.22));
        user.setCreateTime(new Date());
        new Lq<User>(User.class, jdbcTemplate).debug().toSave(user);
//        int i=1/0;

        return user;
    }

    @GetAction("batch/add/user")
    @JsonView
    public String batchAddUser() {
        User user = new User();
        user.setId(7);
        user.setUserName("jack");
        user.setDeptId(1);
        user.setMoney(new BigDecimal(1000.1));
        user.setCreateTime(new Date());

        User user2 = new User();
        user2.setId(8);
        user2.setUserName("jack2");
        user2.setDeptId(2);
        user2.setMoney(new BigDecimal(2000.2));
        user2.setCreateTime(new Date());
        List<User> userList = new ArrayList<>();
        userList.add(user);
        userList.add(user2);

        Integer nums = new Qe(User.class,jdbcTemplate).debug().toBatchInsert(userList);

        return "insert recodes:" + nums;
    }

    @GetAction("user/page")
    @JsonView
    public Page userPage(Page page) {
        page = new Qe<>(User.class,jdbcTemplate).andGe(User::getAge,0).debug().toPage(page);
        return page;
    }

    @GetAction("delUser/{id}")
    @JsonView
    public String delUser(Integer id) {
        int rows = new Lq<User>(User.class, jdbcTemplate).andEq(User::getId,id).debug().toDel();
        return "delete rows : " + rows;
    }

    @GetAction("update")
    @JsonView
    public String update() {
        User user = new User();
        user.setId(1);
        user.setUserName("leeton");
        user.setCreateTime(new Date());
        Lq<User> userLq = new Lq<>(User.class, jdbcTemplate);
        userLq.andEq(User::getId,1).debug().toUpdate(user);
        User u =  userLq.andEq(User::getId,1).debug().to();
        return " Update user: " + u;
    }

}
